import java.util.LinkedList;
import java.util.Queue;

public class Leet101 {
    /**
     * 给定一个二叉树，检查它是否是镜像对称的。
     * 例如，二叉树 [1,2,2,3,4,4,3] 是对称的。
     *     1
     *    / \
     *   2   2
     *  / \ / \
     * 3  4 4  3
     * 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
     *     1
     *    / \
     *   2   2
     *    \   \
     *    3    3
     */
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
        public boolean isSymmetric(TreeNode root) {
            if(root==null||(root.left==null&&root.right==null)){
                return  true;
            }

            LinkedList <TreeNode> linkedList=new LinkedList<>();
            linkedList.add(root.left);
            linkedList.add(root.right);

            while (linkedList.size()>0){

                TreeNode left=linkedList.removeFirst();
                TreeNode right =linkedList.removeFirst();

                if (left==null&&right==null){
                    continue;
                }
                if (left==null||right==null){
                    return  false;
                }
                if (left.val!=right.val){
                    return  false;
                }

                linkedList.add(left.left);
                linkedList.add(right.right);

                linkedList.add(left.right);
                linkedList.add(right.left);
            }
                return  true;
        }
}
